import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
var _excluded = ["render"];

var _dec, _class;

/**
 * @date 2019-03-04
 */
import React from 'react';
import { withRouter, Route } from 'dva/router';
import { connect } from 'dva';
import Exception from "../Exception";
import { extractAccessTokenFromHash, extractRefreshTokenFromHash, setAccessToken, setRefreshToken, getCurrentOrganizationId } from "../../utils/utils";
var AuthorizedRoute = (_dec = connect(), withRouter(_class = _dec(_class = /*#__PURE__*/function (_React$Component) {
  _inherits(AuthorizedRoute, _React$Component);

  var _super = _createSuper(AuthorizedRoute);

  function AuthorizedRoute() {
    var _this;

    _classCallCheck(this, AuthorizedRoute);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      isAuthorized: false,
      isException: false
    };
    return _this;
  }

  _createClass(AuthorizedRoute, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          history = _this$props.history,
          location = _this$props.location;
      var token = extractAccessTokenFromHash(window.location.hash);
      var refreshToken = extractRefreshTokenFromHash(window.location.hash);

      if (token) {
        setAccessToken(token, 60 * 60);
        setRefreshToken(refreshToken, 60 * 60); // 保留上次退出时的页面路径和search

        history.push({
          pathname: location.pathname,
          search: location.search
        });
      }

      dispatch({
        type: 'user/fetchCurrent'
      }).then(function (res) {
        if (res) {
          if (!(res instanceof Error)) {
            // 请求 self 接口成功
            dispatch({
              type: 'global/pubInit',
              payload: {
                language: res.language,
                // 加载菜单国际化
                organizationId: getCurrentOrganizationId()
              }
            }).then(function () {
              _this2.setState({
                isAuthorized: true
              });
            });
          } else {
            // 其他错误
            _this2.setState({
              isException: true
            }); // 清除首屏loading


            var loader = document.querySelector('#loader-wrapper');

            if (loader) {
              document.body.removeChild(loader);
            }

            history.push('/exception/500');
          }
        } // else {
        //   // self 接口 请求 401
        // }

      }); // request(AUTH_SELF_URL).then(user => {
      //   if (user && !isNil(user.id)) {
      //     this.setState({
      //       isAuthorized: true,
      //     });
      //   }
      // })
      // .catch(() => {
      //   removeAccessToken();
      //   window.location.href = AUTH_URL;
      // });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          _render = _this$props2.render,
          rest = _objectWithoutProperties(_this$props2, _excluded);

      var _this$state = this.state,
          isAuthorized = _this$state.isAuthorized,
          isException = _this$state.isException;
      return isAuthorized === true ? /*#__PURE__*/React.createElement(Route, Object.assign({}, rest, {
        render: function render(props) {
          return _render(props);
        }
      })) : isException === true ? /*#__PURE__*/React.createElement(Exception, {
        type: "500"
      }) : null;
    }
  }]);

  return AuthorizedRoute;
}(React.Component)) || _class) || _class);
export { AuthorizedRoute as default };